package com.xunce.xcworkservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunce.xcworkservice.Enums.Enums;
import com.xunce.xcworkservice.entity.Account;
import com.xunce.xcworkservice.response.ECode;
import com.xunce.xcworkservice.response.R;
import com.xunce.xcworkservice.service.AccountService;
import com.xunce.xcworkservice.vo.IPageVo;
import com.xunce.xcworkservice.vo.UserInfoVo;
import com.xunce.xcworkservice.vo.UserSimpleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yanlong.guo
 * 员工账号管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/account")
@Tag(name = "员工管理")
public class AccountController extends BaseController {

    private  final AccountService accountService;

    @Operation(summary = "员工列表", description = "用户管理列表",
            parameters = {
                    @Parameter(name = "name", description = "姓名")
            })
    @GetMapping("/getList")
    public R<IPageVo<UserInfoVo>> getList(){
        Map<String, Object> query = getParamMap();
        IPageVo<UserInfoVo> accountPage = accountService.selectPageByMap(query);
        return  R.success(accountPage);
    }

    @Operation(summary = "员工编辑/新增", description = "有id认为是编辑,无id为新增")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody @Validated Account account){
        accountService.edit(account);
        return R.success();
    }

    @Operation(summary = "员工禁用/开启", description = "禁用员工后无法登录也不会在工时清算中计算数量",
            parameters = {
                @Parameter(name = "accountId", description = "用户id", required = true),
                @Parameter(name = "state", description = "状态1启用 0禁用", required = true)
            })
    @GetMapping("/setState")
    public R<String> setState(
            @RequestParam @NotNull(message = "用户id不能为空") Integer accountId,
            @RequestParam @NotNull(message = "状态不能为空") Integer state)
    {
        if (!Enums.AccountState.ENABLED.getValue().equals(state) && !Enums.AccountState.DISABLED.getValue().equals(state)){
            return R.error(ECode.VALID_ERROR, "状态不合法");
        }
        UpdateWrapper<Account> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Account::getAccountId, accountId)
                .set(Account::getState, state);
        boolean res = accountService.update(wrapper);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "重置密码", description = "重置用户密码",
            parameters = {
                    @Parameter(name = "accountId", description = "用户id", required = true),
                    @Parameter(name = "state", description = "状态1启用 2禁用", required = true)
            })
    @PostMapping("/resetPassword")
    public R<String> resetPassword(
            @RequestParam @NotNull(message = "用户id不能为空") Integer accountId,
            @RequestParam @NotBlank(message = "密码不能为空") String password)
    {

        return R.success();
    }


    @Operation(summary = "根据tag查询员工", description = "不同场景传不同tag值 多个只用,符号隔开 审核:verify ")
    @GetMapping("/getTagAccount")
    public R<List<UserSimpleVo>> getTagAccount(@RequestParam @Schema(description = "tag标识") String tag){
        return R.success(accountService.getTagAccount(tag));
    }
}
